package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetDeceVersion;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.net.busheven.UtlisNetWork;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderAll;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderDpj;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderDqr;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderZyz;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.CircleImageView;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.example.permission.myjbm.activitys.CertificateCenterAty;
import com.example.permission.myjbm.activitys.IntegralCenterAty;
import com.example.permission.myjbm.utils.LoginSharedPreferences;
import com.jbm.jbmsupplier.util.BusinessEntity;
import com.jbm.jbmsupplier.util.LoginEntity;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase.gSocket;

/**
 * 李征
 * Created by 家帮忙 on 2017/1/12.
 */

public class CardHomeActivity extends ActivityUse {
    @InjectView(R.id.nameimage)
    CircleImageView nameimage;
    @InjectView(R.id.mide_jifen)
    TextView mideJifen;
    @InjectView(R.id.mide_gxz)
    TextView mideGxz;
    @InjectView(R.id.mide_open)
    TextView mideOpen;
    @InjectView(R.id.mide_regi)
    TextView mideRegi;
    @InjectView(R.id.mide_sc)
    TextView mideSc;
    @InjectView(R.id.mide_fb)
    TextView mideFb;
    @InjectView(R.id.mide_shezhi)
    TextView mideShezhi;
    @InjectView(R.id.mide_bzzx)
    TextView mideBzzx;
    @InjectView(R.id.te_dingdan_mide)
    TextView teDingdanMide;
    @InjectView(R.id.relati_mide_daifukuan)
    RelativeLayout relatiMideDaifukuan;
    @InjectView(R.id.relati_mide_daishuhuo)
    RelativeLayout relatiMideDaishuhuo;
    @InjectView(R.id.relat_mide_daifahuo)
    RelativeLayout relatMideDaifahuo;
    @InjectView(R.id.namenichegn)
    TextView namenichegn;
    @InjectView(R.id.teweixinname)
    TextView teweixinname;
    @InjectView(R.id.te_dai_f_k_mide)
    TextView teDaiFKMide;
    @InjectView(R.id.te_xiaoxi_mide)
    TextView teXiaoxiMide;
    @InjectView(R.id.te_dai_s_h_mide)
    TextView teDaiSHMide;
    @InjectView(R.id.te_xiaoxi_mide_s)
    TextView teXiaoxiMideS;
    @InjectView(R.id.mide_relayout)
    RelativeLayout mideRelayout;
    private long curren = 0;
    private final int UPDATEMES = 0505;
    private final int MARCHANG = 0606;
    private int mode;
    private LoginSharedPreferences sp;
    private DialogLoading dialogLoading;
    private SharedPreferences mSharedPreferences;

    @Override
    protected int setContentView1() {
        return R.layout.activity_mide;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        mSharedPreferences = getSharedPreferences("sinopecall", Activity.MODE_PRIVATE);
        sp = new LoginSharedPreferences(this);


        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
                    mode = UtlisNetWork.getnetwokstate(CardHomeActivity.this);
                }
            }
        }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    @OnClick({R.id.mide_relayout,R.id.mide_open, R.id.mide_regi, R.id.mide_sc, R.id.mide_fb, R.id.mide_shezhi, R.id.mide_bzzx, R.id.nameimage, R.id.te_dingdan_mide, R.id.relati_mide_daifukuan, R.id.relati_mide_daishuhuo, R.id.relat_mide_daifahuo,R.id.mine_jifen,R.id.mine_lijuan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mide_relayout:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserSet();

                }
                break;
            case R.id.nameimage:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserSet();

                }
                break;
            case R.id.mide_open:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getDoorMessage();
                }
                break;
            case R.id.mide_regi:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getRegiMerchant();
                }
                break;
            case R.id.mide_sc:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getCollect();
                }
                break;
            case R.id.mide_fb:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getIssue();
                }
                break;
            case R.id.mide_shezhi:
                getSet();
                break;
            case R.id.mide_bzzx:
                getHelp();
                break;
            case R.id.te_dingdan_mide:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserqbOrder();
                }
                break;
            case R.id.relati_mide_daifukuan:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserqbOrderSqr();
                }
                break;
            case R.id.relati_mide_daishuhuo:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserqbOrderZyz();
                }
                break;
            case R.id.relat_mide_daifahuo:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    getUserqbOrderDqr();
                }
                break;
            //积分中心
            case R.id.mine_jifen:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    startActivity(new Intent(this, IntegralCenterAty.class));
                }

                break;
            //礼券中心
            case R.id.mine_lijuan:
                if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
                    startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
                } else {
                    if (mode == -1) {
                        hint("网络无连接请链接后重试");
                        return;
                    }
                    startActivity(new Intent(this, CertificateCenterAty.class));
                }

                break;

        }
    }


    public void huoqu() {
        if (mSharedPreferences.getBoolean("IsLogin", false) != true) {
            startActivity(new Intent(CardHomeActivity.this, LoginActivity.class));
        } else {
            String url = mSharedPreferences.getString("urlnames", "");//图片
            String weixin = mSharedPreferences.getString("weinxinlogin", "");//保存是不是微信登录
            String urls = mSharedPreferences.getString("urltu", "");//修改图片之保存
            String name = mSharedPreferences.getString("username", "");//微信登录的昵称
            String phname = mSharedPreferences.getString("phname", "");//手机号登录昵称
            if (weixin.equals("weixin")) {
                nameimage.setImageDrawable(getResources().getDrawable(R.mipmap.pic));
                if (gSocket.nickname != null) {
                    teweixinname.setText(gSocket.nickname);
                }
                if (!name.equals("")) {
                    namenichegn.setText(name);
                } else if (gSocket.logo != null) {
                    Picasso.with(CardHomeActivity.this).load(gSocket.logo).into(nameimage);
                }
            } else if (weixin.equals("shouji")) {
                nameimage.setImageDrawable(getResources().getDrawable(R.mipmap.pic));
                if (gSocket.nickname != null) {
                    teweixinname.setText(gSocket.nickname);
                }
                if (!phname.equals("")) {
                    StringBuffer buff=new StringBuffer();

                    for(int i=0;i<phname.length();i++){
                        char index = phname.charAt(i);
                                if(i<3||i>phname.length()-5){
                                    buff.append(index);
                                }else{
                                    buff.append("*");
                                }
                    }
                    namenichegn.setText(buff.toString());
                } else if (gSocket.logo == null) {

                } else if (gSocket.logo != null) {

                    Picasso.with(CardHomeActivity.this).load(gSocket.logo).into(nameimage);

                }
            }
            if (!urls.equals("")) {
                Picasso.with(CardHomeActivity.this).load(urls).into(nameimage);
            } else {
                return;
            }

        }

    }


    private void getUserSet() {//点击头像设置用户信息
        startActivity(new Intent(getBaseContext(), MyUserSet.class));
    }

    private void getUserqbOrder() {//用户的全部订单
        startActivity(new Intent(getBaseContext(), MyOrderAll.class));
    }

    private void getUserqbOrderSqr() {//用户的待确认订单
        startActivity(new Intent(getBaseContext(), MyOrderDqr.class));
    }

    private void getUserqbOrderZyz() {//用户的作业中订单
        startActivity(new Intent(getBaseContext(), MyOrderZyz.class));
    }

    private void getUserqbOrderDqr() {//用户的待评价订单
        startActivity(new Intent(getBaseContext(), MyOrderDpj.class));
    }

    private void getDoorMessage() {//手机开门设置
        startActivity(new Intent(getBaseContext(), MyMabageDoor.class));
    }

    private void getRegiMerchant() {//注册为商家
//        MyLog.e("注册为商家loginid"+gSocket.login_id+"");
        if (sp.getMerchantState() == false) {
            NetDeceVersion netDeceVersion = new NetDeceVersion(handerl);
//            if (mSharedPreferences.getInt("result.login_id",0)==0){
                netDeceVersion.setMerChant(gSocket.login_id, MARCHANG);
//            }else {
//                netDeceVersion.setMerChant(mSharedPreferences.getInt("result.login_id", 0), MARCHANG);

//            }



            dialogLoading = new DialogLoading(this, "加载中...", true);
            dialogLoading.show();
        } else {
            startActivity(new Intent(getBaseContext(), MyGoods.class));
        }


    }

    private void getCollect() {//我的收藏
        startActivity(new Intent(CardHomeActivity.this, MyLlaciton.class));
    }

    //我的发布
    private void getIssue() {//我的发布
        startActivity(new Intent(CardHomeActivity.this, MyIssue.class));
    }

    private void getSet() {//设置页面
        startActivity(new Intent(CardHomeActivity.this, ActivitySet.class));
    }

    private void getHelp() {//帮助页面
        startActivity(new Intent(CardHomeActivity.this, MyGunYuSM.class));

    }

    @Override
    protected void onResume() {
        super.onResume();
        NetWork netWork = new NetWork(handerl);
//        if (mSharedPreferences.getInt("result.login_id",0)==0){
            netWork.setUpdateMessage(gSocket.login_id, UPDATEMES);
//        }else {
//            netWork.setUpdateMessage(mSharedPreferences.getInt("result.login_id", 0), UPDATEMES);
//        }
            huoqu();
    }

    @Override
    protected void onStart() {
//        handerl.sendMessage(handerl.obtainMessage(WEIXINJEADAMURL));
        super.onStart();
    }

    Handler handerl = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                case UPDATEMES: {
                    LoginEntity busin = (LoginEntity) msg.obj;
                    if (busin.back) {
                        nameimage.setImageDrawable(getResources().getDrawable(R.mipmap.pic));
                        if (busin.getLogo().equals("")) {
                            return;
                        } else {
                            Picasso.with(CardHomeActivity.this).load(busin.getLogo()).into(nameimage);
                            teweixinname.setText(gSocket.nickname);
                        }
                        if (!busin.getNickname().equals("")) {
                            teweixinname.setText(busin.getNickname());
                        }
                    } else {

                    }
                    break;
                }
                case MARCHANG: {
                    BusinessEntity businessEntity = (BusinessEntity) msg.obj;
                    if (businessEntity.back) {
                        //已审
                        if (businessEntity.getStatus() == 1) {
                            if (dialogLoading != null) {
                                dialogLoading.dismiss();
                            }
                            startActivity(new Intent(getBaseContext(), MyGoods.class));
                            sp.saveMerchantState(true);
                        } else {
                            //审核中
                            if (businessEntity.getStatus() == 0) {
                                if (dialogLoading != null) {
                                    dialogLoading.dismiss();
                                }
                                Intent intent = new Intent(getBaseContext(), MyRegistrMessage.class);
                                intent.putExtra("businessEntity", businessEntity);
                                intent.putExtra("state", 0);
                                startActivity(intent);
                                //退回
                            } else if (businessEntity.getStatus() == 2) {
                                if (dialogLoading != null) {
                                    dialogLoading.dismiss();
                                }
                                Intent intent = new Intent(getBaseContext(), MyRegistrMessage.class);
                                intent.putExtra("businessEntity", businessEntity);
                                intent.putExtra("state", 2);
                                startActivity(intent);
                            }

                        }

                    } else {
                        if (dialogLoading != null) {
                            dialogLoading.dismiss();
                        }
                        sp.saveMerchantState(false);
                        startActivity(new Intent(getBaseContext(), MyRegistrMessage.class));

                    }


                }

            }
        }
    };

    @Override
    public void onBackPressed() {

        if ((System.currentTimeMillis() - curren) > 2000) {
            MyToast.show(CardHomeActivity.this, "再按一次退出", 0);
            curren = System.currentTimeMillis();
        } else {
            getSharedPreferences("open_key_judge", Activity.MODE_PRIVATE).edit().clear().commit();
            ExitAppUtlis.getInstance().exit();
        }


    }

}